package com.nettest.service.system;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nettest.dao.system.MenuDao;
import com.nettest.entity.BaseEntity;
import com.nettest.entity.system.Menu;
import com.nettest.entity.system.Role;

@Service
@Transactional
public class MenuService {
	@Autowired
	private MenuDao menuDao;
	public List<Menu> getTopMenu(Set<Role> roleIds) throws Exception {
		Set<Criterion> set = new HashSet<Criterion>();
		set.add(Restrictions.eq("parentId","0"));
						
		Set<Order> orderSet = new HashSet<Order>();
		orderSet.add(Order.asc("menuId"));
		
		List<Menu> result = menuDao.query(set,orderSet);
		return result;
	}
	public List<Menu> getShortcutMenu() throws Exception {
		Set<Criterion> set = new HashSet<Criterion>();
		set.add(Restrictions.eq("isShortcut",Boolean.TRUE));
		set.add(Restrictions.isNotNull("menuAction"));
		
		Set<Order> orderSet = new HashSet<Order>();
		orderSet.add(Order.asc("menuId"));
		
		List<Menu> result = menuDao.query(set,orderSet);
		return result;
	}
	
	public BaseEntity save(BaseEntity entity) throws Exception {
		return menuDao.save((Menu)entity);
	}
	
	public List<Menu> getSubMenu(String menuId) {
		Set<Criterion> set = new HashSet<Criterion>();
		set.add(Restrictions.eq("parentId",menuId));
						
		Set<Order> orderSet = new HashSet<Order>();
		orderSet.add(Order.asc("menuId"));
		
		List<Menu> result = menuDao.query(set,orderSet);
		return result;
	}

	public List<Menu> getAllSubMenu(String menuId) {
		Set<Criterion> set = new HashSet<Criterion>();
		set.add(Restrictions.ilike("parentId",menuId,MatchMode.START));
		
		Set<Order> orderSet = new HashSet<Order>();
		orderSet.add(Order.asc("menuId"));
		
		List<Menu> result = menuDao.query(set,orderSet);
		return result;
	}
	public List<Menu> getAllMenu() {
		Set<Criterion> set = new HashSet<Criterion>();
						
		Set<Order> orderSet = new HashSet<Order>();
		orderSet.add(Order.asc("menuId"));
		orderSet.add(Order.asc("parentId"));
		
		List<Menu> result = menuDao.query(set,orderSet);
		return result;
	}

	public String selectMaxMenuId(String nodeId) {
		return menuDao.selectMaxMenuId(nodeId);
	}

	public List<Menu> selectChildByParentId(String parentId) {
		Set<Criterion> set = new HashSet<Criterion>();
		set.add(Restrictions.eq("parentId",parentId));
		
		List<Menu> result = menuDao.query(set);
		return result;
	}

	public void deleteMenuById(String parentId) {
		Set<Criterion> set = new HashSet<Criterion>();
		set.add(Restrictions.eq("menuId",parentId));
		
		List<Menu> result = menuDao.query(set);
		if(result != null && !result.isEmpty()) {
			Menu menu = result.get(0);
			menuDao.delete(menu.getId());
		}
	}

	public Menu getMenuById(String menuId) {
		List<Menu> list = menuDao.findBySql("select r from Menu r where r.menuId = ?",menuId);
		Menu r = null;
		if(list != null && !list.isEmpty()) {
			r = list.get(0);
		}
		return r;
	}

	public List<String> getRoleNameByMenuId(String menuId) {
		Menu menu = getMenuById(menuId);
		Set<Role> roles = menu.getRoles();
		List<String> roleNames = new ArrayList<String>();
		for (Role role : roles) {
			roleNames.add(role.getRoleName());
		}
		return roleNames;
	}
	public List<Menu> getAllParentMenu(String parentMenuId) {
		Set<Criterion> set = new HashSet<Criterion>();
		set.add(Restrictions.ilike("parentId",parentMenuId,MatchMode.START));
						
		Set<Order> orderSet = new HashSet<Order>();
		orderSet.add(Order.asc("menuId"));
		
		List<Menu> result = menuDao.query(set,orderSet);
		return result;
	}
	public Menu getRootMenu(String menuId) {
		Set<Criterion> set = new HashSet<Criterion>();
		set.add(Restrictions.ilike("menuId",menuId.substring(0, 3),MatchMode.START));
		set.add(Restrictions.eq("parentId","0"));
						
		Set<Order> orderSet = new HashSet<Order>();
		orderSet.add(Order.asc("menuId"));
		
		List<Menu> result = menuDao.query(set,orderSet);
		if(!result.isEmpty()) {
			return result.get(0);
		}
		return null;
	}
}
